<template>
  <div>
    <!--        搜索框-->
    <div class="pd-5">
      <el-input style="width: 200px" placeholder="请输入菜单名称" suffix-icon="el-icon-search" v-model="query.name"
                class="ml-5 mr-5"></el-input>
      <el-button type="primary" @click="loadBtn">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
      <div style="float: right">
        <el-button type="primary" @click="saveBtn()">新增<i class="el-icon-circle-plus-outline"></i></el-button>
        <el-popconfirm
            class="mr-5 ml-5"
            confirm-button-text='确定'
            cancel-button-text='点错了'
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @confirm="delNumBtn()">
          <el-button type="danger" slot="reference">批量删除<i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>
      </div>
    </div>

    <!--        列表-->
    <el-table :data="tableData" stripe border :header-cell-class-name="headerBg"
              default-expand-all row-key="id"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="编号" width="80px"></el-table-column>
      <el-table-column prop="name" label="菜单名称"></el-table-column>
      <el-table-column prop="path" label="路径" ></el-table-column>
      <el-table-column label="图标" width="130" align="center">
        <template slot-scope="scope">
          <i :class="scope.row.icon" style="font-size: 20px"></i>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述"></el-table-column>
      <el-table-column label="操作"
                       align="center"
                       fixed="right"
                       width="280px">
        <template slot-scope="scope">
          <el-button type="success" @click="saveBtn(scope.row.id)" v-if="!scope.row.pid && !scope.row.path">新增子菜单<i
              class="el-icon-circle-plus-outline"></i>
          </el-button>
          <el-popconfirm
              class="mr-5 ml-5"
              confirm-button-text='确定'
              cancel-button-text='点错了'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="delectBtn(scope.row.id)">
            <el-button type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>

          <el-button type="warning" @click="editBtn(scope.row)">编辑<i class="el-icon-edit"></i></el-button>
        </template>
      </el-table-column>
    </el-table>


    <!--添加用户-->
    <el-dialog :title='otherData.windowName' :visible.sync="dialogFormVisible" width="400px">
      <el-form label-width="80px">
        <el-form-item label="菜单名称">
          <el-input v-model="menu.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单地址">
          <el-input v-model="menu.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-select v-model="menu.icon" clearable placeholder="请选择" style="width: 100%">
            <el-option v-for="item in options" :key="item.value" :label="item.name" :value="item.value">
              <i :class="item.value"> {{ item.name }}</i>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="menu.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
export default {
  data() {
    return {
      //模态窗口下拉框数据
      options: [],

      // 模态窗口样式
      menu: {
        name: '',
        path: '',
        icon: '',
        description: '',
        pid: 0,
      },

      // 条件查询数据
      query: {
        name: '',
      },
      // 主页面数据
      tableData: [],
      // 批量删除数据
      multipleSelection: [],

      //编辑数据
      editBtnData: false,

      headerBg: 'headerBg',
      dialogFormVisible: false,

      otherData: {
        windowName: '',
      }
    }
  },
  methods: {
    //加载
    load() {
      this.request.get("/menu", {
        params: {
          name: this.query.name,
        }
      }).then(res => {
            if (res.code == 200) {
              this.tableData = res.data
            }
          },
          Error => {
            console.log(Error)
          }
      )
    },
    //重置
    reset() {
      this.query.name = ''
      this.load()
    },
    //搜索
    loadBtn() {
      //需将页面转为第一页，从头开始查询
      this.pageNum = 1
      this.load()
    },
    //添加
    saveBtn(pid) {
      this.otherData.windowName = '添加菜单'
      this.dialogFormVisible = true
      this.menu = {}
      if (pid) {
        this.menu.pid = pid
      }
      this.loadOptions()
    },
    save() {
      this.request.post('/menu', this.menu).then(
          res => {
            if (res.code == 200) {
              this.load()
            }
          },
          Error => {
            console.log(Error)
          }
      )
      this.dialogFormVisible = false
    },
    //删除
    delectBtn(id) {
      this.request.delete('/menu/' + id).then(
          res => {
            if (res.code == 200) {
              this.$message.success("删除成功")
              this.load()
            }
          },
          Error => {
            this.$message.success("删除失败")
            console.log(Error)
          }
      )
    },
    //删除多个
    delNumBtn() {
      let ids = this.multipleSelection.map(v => v.id)
      console.log(ids)
      if (ids.length == 0) {
        this.$message.error("请选择要删除的用户")
        return;
      }
      this.request.delete('/menu/del/batch', {data: ids}).then(res => {
            if (res) {
              if (res.code == 200) {
                this.$message.success("删除成功")
                this.load()
              }
            } else {
              this.$message.error("删除失败")
              console.log(Error)
            }
          }
      )
    },
    //多选框事件
    handleSelectionChange(val) {
      this.multipleSelection = val;
      // console.log(val)
    },
    //编辑
    editBtn(row) {
      this.otherData.windowName = '编辑菜单'
      this.editBtnData = true
      this.menu = row
      this.dialogFormVisible = true
      this.loadOptions()
    },
    //模态窗口的下来框数据
    loadOptions() {
      this.request.get("/dict/icon").then(res => {
            if (res.code == 200) {
              this.options = res.data
            }
          },
          Error => {
            console.log(Error)
          }
      )
    },
  },
  created() {
    this.load()
  },
}
</script>


<style scoped>

</style>